using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using AjaxControlToolkit;
using MBLocale;

/// <summary>
/// common class for parameters used in report and dashboard pages.
/// </summary>
public class BuildParameters
{
	public BuildParameters()
	{
		//
		// TODO: Add constructor logic here
		//
	}

    /// <summary>
    /// function to create parameters
    /// </summary>
    /// <param name="bClose">True for Dashboards</param>
    public HtmlTable createParameters(string sqlQry, string sqlParam, Control ctrlPage, string widgetAfRowId,bool bClose)
    {
        HtmlTable tblParam = new HtmlTable();
        tblParam.Border = 0;
        tblParam.CellPadding = 0;
        tblParam.CellSpacing = 0;

        if (bClose)
        {
            tblParam.CellPadding = 0;
            tblParam.CellSpacing = 0;
            tblParam.Border = 0;
            tblParam.Width = "100%";
        }
        try
        {
            DynamicDisplayI ddI = new DynamicDisplayI();
            Hashtable hsParam = new Hashtable();
            ArrayList alOrder = new ArrayList();
            ArrayList alOrderIndex = new ArrayList();

            if (sqlParam.Trim() == "")
            {
                if (sqlQry.Contains("$") || sqlQry.Contains("#"))
                {
                    getParameters("$", ref hsParam, sqlQry);
                    getParameters("#", ref hsParam, sqlQry);
                }
            }
            else
            {
                if (sqlParam.Trim().ToLower().StartsWith("parameters"))
                    sqlParam = sqlParam.Substring("parameters".Length + 1).Trim();
                int PCount = 0; // Added by 'Imran Gani' on 05-Jan-2014, to get count of parameters
                foreach (string str1 in sqlParam.Split(','))
                {
                    if (str1.Trim() == "") continue;
                    PCount++;
                    string sParameter = getParameterKey(str1.Trim(), ref alOrderIndex); // Added by 'Imran Gani' on 28-Oct-2013, to get parameter key for hashtable.
                    if (sParameter.IndexOf("=") == -1)
                    {
                        hsParam.Add(sParameter.Trim(), getDefaultValue(sParameter.Substring(sParameter.IndexOf("]") + 1).Trim()));
                        alOrder.Add(str1.Trim());
                    }
                    else
                    {
                        hsParam.Add(sParameter.Substring(0, sParameter.IndexOf("=")).Trim(), sParameter.Substring(sParameter.IndexOf("=") + 1).Trim());
                        alOrder.Add(str1.Substring(0, str1.IndexOf("=")).Trim());
                    }
                    if (sParameter.IndexOf("=") == -1) // Added by 'Imran Gani' on 01-Aug-2013, to handle parameter values from URL
                        getParameterValueFromURL(ref hsParam, sParameter.Substring(0, sParameter.LastIndexOf("]") + 1).Trim(), widgetAfRowId, PCount);
                    else
                        getParameterValueFromURL(ref hsParam, sParameter.Substring(0, sParameter.IndexOf("=")).Trim(), widgetAfRowId, PCount);
                }
            }
            if (hsParam.Count > 0)
            {
                ReplaceSysVariables(ref hsParam); // Added by 'Imran Gani' on 09-Jul-2012, to replace the system variables with their values.
                alOrder = getParameterOrder(alOrder, alOrderIndex); // Added by 'Imran Gani' on 28-Oct-2013, to arrange parameter order by sequence.
                HtmlTableRow htRow = new HtmlTableRow();
                HtmlTableCell htCell = new HtmlTableCell();
                HtmlTable httTable = new HtmlTable();
                httTable.Border = 0;
                httTable.CellPadding = 3;
                //if (bClose)
                    //httTable.CellPadding = 0;
                httTable.CellSpacing = 0;

                //httTable = new HtmlTable();
                //httTable.Border = 1;
                //httTable.CellPadding = 3;
                ////if (bClose)
                //    //httTable.CellPadding = 0;
                //httTable.CellSpacing = 0;

                
                HtmlTableRow httRow = new HtmlTableRow();
                HtmlTableCell httCell1 = new HtmlTableCell();
                HtmlTableCell httCell2 = new HtmlTableCell();
                Label lbl1 = new Label();
                Label lbl2 = new Label();
                if (bClose)
                    CreateParameterHeader(ref htCell, widgetAfRowId);
                else
                {
                    
                    //lbl1.Text = "Parameter";
                    //lbl1.CssClass = "controlLabelFont";
                    
                    //lbl2.Text = "Value";
                    //lbl2.CssClass = "controlLabelFont";
                    
                    //httCell1.Controls.Add(lbl1);
                    ////httCell1.Attributes.Add("class", "displaygridbottom");
                    //httCell2.Controls.Add(lbl2);
                    ////httCell2.Attributes.Add("class", "displaygridbottom");
                    //httRow.Cells.Add(httCell1);
                    //httRow.Cells.Add(httCell2);
                    //httTable.Rows.Add(httRow);
                    //htCell.Controls.Add(httTable);
                }
                if (alOrder.Count == 0)
                {
                    #region Old parameter style
                    int iCnt = 1;
                    foreach (string key in hsParam.Keys)
                    {
                        if (key.Trim().StartsWith("$") || key.Trim().StartsWith("#"))
                        {
                            string ch = key.StartsWith("$") ? "$" : "#";
                            lbl1 = new Label();
                            lbl1.Text = key.Replace(ch, "");
                            lbl1.CssClass = "controlLabelFont";
                            lbl1.ID = "lbl_" + key + widgetAfRowId;
                            httRow = new HtmlTableRow();
                            httCell1 = new HtmlTableCell();
                            httCell1.Align = "left";
                            httCell1.NoWrap = true;
                            httCell1.Controls.Add(lbl1);
                            if(bClose)
                                httCell1.Attributes.Add("class", "Grid_Row_actions");
                            httRow.Cells.Add(httCell1);
                            TextBox txt = new TextBox();
                            txt.ID = "txt_param_" + iCnt.ToString() + widgetAfRowId;
                            txt.CssClass = "controlFont controlWidth";
                            httCell2 = new HtmlTableCell();
                            if(bClose)
                                httCell2.Attributes.Add("class", "Grid_Row_actions");
                            httCell2.Controls.Add(txt);
                            httRow.Cells.Add(httCell2);
                            httTable.Rows.Add(httRow);
                            if (key.StartsWith("#"))
                            {
                                if (!bClose)
                                    addAjaxCalendarDate(txt.ID, ref httCell2);
                                else
                                    addAjaxCalendarDate(txt.ID, ref httCell2, true);
                            }
                            iCnt++;
                        }
                    }
                    #endregion
                }
                else
                {
                    #region new parameter style
                    int ParamCount = 0;
                    foreach (string key in alOrder)
                    {
                        string parName = key.Substring(0, key.LastIndexOf("["));
                        string par = key.Replace(parName, "").Replace("[", "").Replace("]", "");
                        string[] pars = par.Split('.');
                        HDDatabase hdd = new HDDatabase();
                        DataTable dt = hdd.GetDataTable("select * from sy_pg_ctr where page_id=(select af_row_id from sy_pg where [page_table_name]='" + pars[0] + "') and control_attrib_id='" + pars[1] + "'");
                        if (dt.Rows.Count > 0)
                        {
                            ParamCount++;
                            lbl1 = new Label();
                            lbl1.Text = parName.Replace("[", "").Replace("]", "");// dt.Rows[0]["control_name"].ToString();
                            lbl1.CssClass = "controlLabelFont";
                            lbl1.ID = "lbl_" + dt.Rows[0]["af_row_id"].ToString() + "_" + widgetAfRowId + "_" + ParamCount.ToString(); // Modified by 'Imran Gani' on 05-Jan-2014, replace 'key' => 'Control_row_id'
                            httRow = new HtmlTableRow();
                            httCell1 = new HtmlTableCell();
                            httCell1.Align = "left";
                            httCell1.NoWrap = true;
                            httCell1.Controls.Add(lbl1);
                            if (bClose)
                                httCell1.Attributes.Add("class", "Grid_Row_actions");
                            httRow.Cells.Add(httCell1);

                            //creating control based on control_html
                            //EM,DD,TA,AN,TB,CB,DDE,PW,ENC

                            httCell2 = new HtmlTableCell();
                            httCell2.NoWrap = true;
                            switch (dt.Rows[0]["control_html"].ToString().ToUpper())
                            {
                                case "AN":
                                case "EM":
                                case "PW":
                                case "ENC":
                                case "TA":
                                case "TB":
                                    TextBox txt = new TextBox();
                                    txt.ID = "txt_" + dt.Rows[0]["af_row_id"].ToString() + "_" + widgetAfRowId + "_" + ParamCount.ToString(); // Modified by 'Imran Gani' on 05-Jan-2014, replace 'key' => 'Control_row_id'
                                    txt.CssClass = "controlFont controlWidth";
                                    
                                    if(dt.Rows[0]["control_bind_id"].ToString().Trim()!="") //Added by Srivatsan for Getting bind value
                                    {
                                        
                                    }

                                    if (dt.Rows[0]["control_html"].ToString().ToUpper() == "TA")
                                        txt.TextMode = TextBoxMode.MultiLine;
                                    if (hsParam[key].ToString().Trim() == "''" || (hsParam[key].ToString().Trim() == "0"))
                                        txt.Text = "";
                                    else
                                        txt.Text = hsParam[key].ToString().Replace("''", "'"); // string replaced by 'Imran Gani' on 29-Oct-12, to double codes.

                                    httCell2.Controls.Add(txt);
                                    if (dt.Rows[0]["control_html"].ToString().ToUpper() == "PW")
                                    {
                                        txt.TextMode = TextBoxMode.Password;
                                        Literal l = new Literal();
                                        l.Text = "&nbsp;<img src=\"Images/key.gif\" alt=\"Password Field\" border=\"0\" />";
                                        httCell2.Controls.Add(l);
                                    }
                                    if (dt.Rows[0]["control_bind_id"].ToString() != "")
                                    {
                                        DataTable dtTmp = hdd.GetDataTable("select * from sy_bnd where af_row_id = '" + dt.Rows[0]["control_bind_id"].ToString() + "';");
                                        if (dtTmp.Rows.Count > 0)
                                        {
                                            Image imgParam = new Image();
                                            imgParam.ID = "imglookup_" + dt.Rows[0]["af_row_id"].ToString() + "_" + widgetAfRowId + "_" + ParamCount.ToString(); // Modified by 'Imran Gani' on 28-Jan-2014,
                                            imgParam.ImageUrl = "~/Images/search.gif";
                                            imgParam.ToolTip = "Click here for Lookup Window";
                                            imgParam.CssClass = "SearchPopup";
                                            httCell2.Controls.Add(imgParam);

                                            HiddenField hfTxt = new HiddenField();
                                            httCell2.Controls.Add(hfTxt);
                                            hfTxt.ID = "hf_" + dt.Rows[0]["af_row_id"].ToString() + "_" + widgetAfRowId + "_" + ParamCount.ToString(); // Modified by 'Imran Gani' on 05-Jan-2014, replace 'key' => 'Control_row_id'
                                            if (bClose)
                                            {
                                                imgParam.Attributes.Add("onclick", "javascript:openlookupwindow('" + hfTxt.ID + "','" + txt.ID + "','" + dt.Rows[0]["control_bind_id"].ToString() + "','','');");
                                                txt.Attributes.Add("onchange", "javascript:GetBindValue('" + dt.Rows[0]["control_bind_id"].ToString() + "','ctl00_ContentPlaceHolder3_"+hfTxt.ID+"','ctl00_ContentPlaceHolder3_"+txt.ID+"')");
                                            }
                                            else
                                                imgParam.Attributes.Add("onclick", "javascript:openlookupwindow('wz_Report_" + hfTxt.ID + "','wz_Report_" + txt.ID + "','" + dt.Rows[0]["control_bind_id"].ToString() + "','','');");//wz_Report_
                                            
                                            DataTable dt_bind = new DataTable(); // Added by 'Imran Gani' on 22-Jan-2014, for bind values.
                                            BindControl bc = new BindControl();
                                            dt_bind = hdd.GetDataTable(bc.GetBindSql(dt.Rows[0]["control_bind_id"].ToString()));
                                            bc.HandleLocalizedBindText(dt_bind, dt.Rows[0]["control_bind_id"].ToString());
                                            if (dt_bind.Rows.Count > 0)
                                            {
                                                //DataRow[] drBind = dt_bind.Select("Val = '" + txt.Text + "'");
                                                //if (drBind.Length > 0)
                                                //{
                                                //    hfTxt.Value = txt.Text;
                                                //    txt.Text = drBind[0]["Fld"].ToString();
                                                //}
                                                DataRow[] drBind = dt_bind.Select("Fld = '" + txt.Text.Replace("'", "''") + "'");
                                                if (drBind.Length > 0)
                                                {
                                                    hfTxt.Value = drBind[0]["Val"].ToString();
                                                    txt.Text = drBind[0]["Fld"].ToString();
                                                }
                                            }
                                        }
                                    }
                                    if (dt.Rows[0]["control_attrib_type"].ToString().ToUpper() == "DA")
                                    {
                                        char[] trimCh = { '\'' };
                                        if (txt.Text.StartsWith("'") && txt.Text.EndsWith("'"))
                                            txt.Text = txt.Text.TrimStart(trimCh).TrimEnd(trimCh);
                                        if (txt.Text != "") // Added by 'Imran Gani' on 19-Mar-2014, for MBLocale
                                            txt.Text = MBLocalization.getLocalizedDate(HttpContext.Current.Session["userlocale"].ToString(), DateTime.Parse(txt.Text));
                                        //ce.OnClientShowing = "hideC1Report";
                                        //ce.OnClientHidden = "showC1Report";
                                        //txt.Attributes.Add("onfocus", "javascript:hideC1Report()");
                                        //txt.Attributes.Add("onblur", "javascript:showC1Report()");
                                        if (!bClose)
                                            addAjaxCalendarDate(txt.ID, ref httCell2);
                                        else
                                            addAjaxCalendarDate(txt.ID, ref httCell2, true);
                                    }
                                    else if (dt.Rows[0]["control_attrib_type"].ToString().ToUpper() == "DT")
                                    {
                                        char[] trimCh = { '\'' };
                                        if (txt.Text.StartsWith("'") && txt.Text.EndsWith("'"))
                                            txt.Text = txt.Text.TrimStart(trimCh).TrimEnd(trimCh);
                                        if (txt.Text != "") // Added by 'Imran Gani' on 19-Mar-2014, for MBLocale
                                            txt.Text = MBLocalization.getLocalizedDateTime(HttpContext.Current.Session["userlocale"].ToString(), DateTime.Parse(txt.Text));
                                        //txt.Attributes.Add("onfocus", "javascript:hideC1Report()");
                                        //txt.Attributes.Add("onblur", "javascript:showC1Report()");
                                        if (!bClose)
                                            addAjaxCalendarDateTime(txt.ID, ref httCell2);
                                        else
                                            addAjaxCalendarDateTime(txt.ID, ref httCell2, true);
                                    }
                                    break;
                                case "CB":
                                    CheckBox cb = new CheckBox();
                                    cb.ID = "cb_" + dt.Rows[0]["af_row_id"].ToString() + "_" + widgetAfRowId + "_" + ParamCount.ToString(); // Modified by 'Imran Gani' on 05-Jan-2014, replace 'key' => 'Control_row_id'
                                    cb.CssClass = "controlFont controlWidth";
                                    cb.Checked = Convert.ToBoolean(hsParam[key].ToString().Replace("'", ""));
                                    httCell2.Controls.Add(cb);
                                    break;
                                case "DD":
                                case "DDE":
                                    DropDownList dd = new DropDownList();
                                    dd.CssClass = "controlFont controlWidth";
                                    dd.ID = "dd_" + dt.Rows[0]["af_row_id"].ToString() + "_" + widgetAfRowId + "_" + ParamCount.ToString(); // Modified by 'Imran Gani' on 05-Jan-2014, replace 'key' => 'Control_row_id'
                                    //dd.Text = hsParam[key].ToString(); // Commented by 'Imran Gani' on 01-Aug-2013
                                    httCell2.Controls.Add(dd);
                                    if (dt.Rows[0]["control_bind_id"].ToString() != "")
                                    {
                                        DataTable dtTmp = hdd.GetDataTable("select * from sy_bnd where af_row_id = '" + dt.Rows[0]["control_bind_id"].ToString() + "';");
                                        if (dtTmp.Rows.Count > 0)
                                        {
                                            BindControl bc = new BindControl();
                                            bc.Bind(dt.Rows[0]["control_bind_id"].ToString(), dd, ref dtTmp, dt.Rows[0]);
                                        }
                                    }
                                    dd.SelectedValue = hsParam[key].ToString().Replace("'", ""); // Added by 'Imran Gani' on 01-Aug-2013
                                    break;
                                case "DT":
                                case "DA":
                                    TextBox dadt = new TextBox();
                                    dadt.CssClass = "controlFont controlWidth";
                                    dadt.Text = hsParam[key].ToString();
                                    dadt.ID = "dt_" + dt.Rows[0]["af_row_id"].ToString() + "_" + widgetAfRowId + "_" + ParamCount.ToString(); // Modified by 'Imran Gani' on 05-Jan-2014, replace 'key' => 'Control_row_id'
                                    if (dt.Rows[0]["control_html"].ToString().ToUpper() == "DA")
                                    {
                                        if (!bClose)
                                            addAjaxCalendarDate(dadt.ID, ref httCell2);
                                        else
                                            addAjaxCalendarDate(dadt.ID, ref httCell2, true);
                                    }
                                    else
                                    {
                                        if (!bClose)
                                            addAjaxCalendarDateTime(dadt.ID, ref httCell2);
                                        else
                                            addAjaxCalendarDateTime(dadt.ID, ref httCell2);
                                    }
                                    break;
                                case "DL": // Added by 'Imran Gani' on 28-Jan-2014, for droplist support.
                                    DropDownList DDL_Droplist = new DropDownList();
                                    DDL_Droplist.CssClass = "controlFont controlWidth";
                                    DDL_Droplist.ID = "dd_" + dt.Rows[0]["af_row_id"].ToString() + "_" + widgetAfRowId + "_" + ParamCount.ToString();
                                    
                                    DataTable dt_Droplist = hdd.GetDataTable("select * from [sy_pg_ctr_droplist] where [page_id] = '" + dt.Rows[0]["page_id"].ToString() + "' and [control_id] = '" + dt.Rows[0]["af_row_id"].ToString() + "';");
                                    foreach (DataRow dr in dt_Droplist.Select("droplist_value in ('none','empty')"))
                                        dr["droplist_value"] = "";
                                    DDL_Droplist.DataSource = dt_Droplist;
                                    DDL_Droplist.DataTextField = "droplist_value";
                                    DDL_Droplist.DataValueField = "droplist_value";
                                    DDL_Droplist.DataBind();
                                    #region New Commented code Added by 'Imran Gani' on 19-Mar-2014, to use switch case with MBLocale
                                    //foreach (DataRow dr in dt_Droplist.Rows) // Added by 'Imran Gani' on 19-Mar-2014, for MBLocale
                                    //{
                                    //    string sDLText = dr["droplist_value"].ToString();
                                    //    try
                                    //    {
                                    //        switch (dt.Rows[0]["control_attrib_type"].ToString().ToLower())
                                    //        {
                                    //            case "dt":
                                    //            case "da":
                                    //                sDLText = MBLocalization.getLocalizedDate(HttpContext.Current.Session["userlocale"].ToString(), DateTime.Parse(dr["droplist_value"].ToString()));
                                    //                break;
                                    //            case "nu":
                                    //            case "an":
                                    //                sDLText = MBLocalization.getLocalizedInteger(HttpContext.Current.Session["userlocale"].ToString(), int.Parse(dr["droplist_value"].ToString()));
                                    //                break;
                                    //            case "cu":
                                    //            case "dc":
                                    //            case "fl":
                                    //                sDLText = MBLocalization.getLocalizedDecimal(HttpContext.Current.Session["userlocale"].ToString(), double.Parse(dr["droplist_value"].ToString()));
                                    //                break;
                                    //        }
                                    //    }
                                    //    catch (Exception ex)
                                    //    {
                                    //        sDLText = dr["droplist_value"].ToString();
                                    //        dr["droplist_value"] = new Localization().getDBValueFromLocale(dr["droplist_value"].ToString(), dt.Rows[0]["control_attrib_type"].ToString());
                                    //    }
                                    //    DDL_Droplist.Items.Add(new ListItem(sDLText, dr["droplist_value"].ToString()));
                                    //}
                                    #endregion
                                    httCell2.Controls.Add(DDL_Droplist);
                                    if (hsParam[key].ToString().Replace("'", "") != "" && DDL_Droplist.Items.FindByValue(hsParam[key].ToString().Replace("'", "")) != null)
                                        DDL_Droplist.Items.FindByValue(hsParam[key].ToString().Replace("'", "")).Selected = true;
                                    break;
                            }
                            if (bClose)
                                httCell2.Attributes.Add("class", "Grid_Row_actions");
                            httRow.Cells.Add(httCell2);
                            httTable.Rows.Add(httRow);
                        }
                        dt = null;
                    }
                    #endregion
                }
                htCell.Controls.Add(httTable);
                htRow.Cells.Add(htCell);
                tblParam.Rows.Add(htRow);
            }
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("BuildParameters: createParameters : Error : " + ex.Message);
        }
        return tblParam;
    }

    private void addAjaxCalendarDate(string id, ref HtmlTableCell htc)
    {
        if (id.Length < 1) return;
        try
        {
            CalendarExtender ce = new CalendarExtender();
            ce.ID = "ce_ajax_" + id;
            //ce.Format = getUserDateFormat();
            ce.Format = MBLocalization.getDateFormat(HttpContext.Current.Session["userlocale"].ToString()); // Added by 'Imran Gani' on 19-Mar-2014, for MBLocale
            ce.TargetControlID = id;
            ce.OnClientShowing = "hideC1Report";
            //ce.OnClientHiding = "showC1Report"; // Commanded by 'Imran Gani' on 22-Apr-2013, to hide report while changing date
            htc.Controls.Add(ce);
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("BuildParameters: addAjaxCalendarDate: Error : " + ex.Message);
        }
    }

    private void addAjaxCalendarDate(string id, ref HtmlTableCell htc,bool bDashboard)
    {
        if (id.Length < 1) return;
        try
        {
            CalendarExtender ce = new CalendarExtender();
            ce.ID = "ce_ajax_" + id;
            //ce.Format = getUserDateFormat();
            ce.Format = MBLocalization.getDateFormat(HttpContext.Current.Session["userlocale"].ToString()); // Added by 'Imran Gani' on 19-Mar-2014, for MBLocale
            ce.TargetControlID = id;
            htc.Controls.Add(ce);
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("BuildParameters: addAjaxCalendarDate: Error : " + ex.Message);
        }
    }

    private void addAjaxCalendarDateTime(string id, ref HtmlTableCell htc)
    {
        if (id.Length < 1) return;
        try
        {
            CalendarExtender ce = new CalendarExtender();
            ce.ID = "ce_ajax_" + id;
            //ce.Format = getUserDateFormat() + " hh:mm tt";
            ce.Format = MBLocalization.getDateTimeFormat(HttpContext.Current.Session["userlocale"].ToString()); // Added by 'Imran Gani' on 19-Mar-2014, for MBLocale
            ce.OnClientShowing = "hideC1Report";
            //ce.OnClientHiding = "showC1Report"; // Commanded by 'Imran Gani' on 22-Apr-2013, to hide report while changing date
            ce.TargetControlID = id;
            htc.Controls.Add(ce);
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("BuildParameters: addAjaxCalendarDateTime: Error : " + ex.Message);
        }
    }
    private void addAjaxCalendarDateTime(string id, ref HtmlTableCell htc,bool bDashboard)
    {
        if (id.Length < 1) return;
        try
        {
            CalendarExtender ce = new CalendarExtender();
            ce.ID = "ce_ajax_" + id;
            //ce.Format = getUserDateFormat() + " hh:mm tt";
            ce.Format = MBLocalization.getDateTimeFormat(HttpContext.Current.Session["userlocale"].ToString()); // Added by 'Imran Gani' on 19-Mar-2014, for MBLocale
            ce.TargetControlID = id;
            htc.Controls.Add(ce);
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("BuildParameters: addAjaxCalendarDateTime: Error : " + ex.Message);
        }
    }

    private string getDefaultValue(string object_field_id)
    {
        string retValue = "''";
        try
        {
            string object_table = object_field_id.Replace("[", "").Substring(0, object_field_id.Replace("[", "").IndexOf("."));
            string field_id = object_field_id.Substring(object_field_id.IndexOf(".") + 1).Replace("]", "");

            HDDatabase hdd = new HDDatabase();
            DataTable dt = hdd.GetDataTable("select control_attrib_type from sy_pg_ctr where control_id='" + field_id + "' and page_id=(select af_row_id from sy_pg where page_table_name='" + object_table + "')");
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0]["control_attrib_type"].ToString().ToUpper() == "TX")
                    retValue = "''";
                else if (dt.Rows[0]["control_attrib_type"].ToString().ToUpper() == "DA" || dt.Rows[0]["control_attrib_type"].ToString().ToUpper() == "DT")
                    retValue = "'" + DateTime.Now.ToShortDateString() + "'";
                else if (dt.Rows[0]["control_attrib_type"].ToString().ToUpper() == "BO")
                    retValue = "'false'";
                else
                    retValue = "0";
            }
            dt = null;
            hdd = null;
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("BuildParameters: getDefaultValue: Error : " + ex.Message);
        }
        return retValue;
    }

    private Hashtable getParameters(string sChar, ref Hashtable hsParam, string sSql)
    {
        try
        {
            int iPos = 0;
            while (iPos < sSql.Length & iPos != -1)
            {
                string sParam = "";
                iPos = sSql.IndexOf(sChar, iPos);
                if (iPos != -1)
                {
                    int iEndPos = sSql.IndexOf(sChar, iPos + 1);
                    if (iEndPos == -1)
                        iEndPos = sSql.Length - iPos;
                    else
                        iEndPos = iEndPos - (iPos - 1);
                    sParam = sSql.Substring(iPos, iEndPos);
                    sParam = sParam.TrimEnd('\'');
                    if (!hsParam.Contains(sParam))
                    {
                        if (sChar == "$")
                            hsParam.Add(sParam, "'0'");
                        else if (sChar == "#")
                            hsParam.Add(sParam, "'" + DateTime.Now.ToShortDateString() + "'");
                    }
                    iPos = iPos + iEndPos;
                }
            }
        }
        catch (Exception Ex)
        {
            LogWriter.WriteLog("BuildParameters: getParameters : Error : " + Ex.Message);
        }
        return hsParam;
    }

    /// <summary>
    /// function to get the parameters values into hashtable
    /// </summary>
    /// <param name="sql"></param>
    /// <param name="param"></param>
    /// <returns></returns>
    public Hashtable updateParameters(string sqlQry, string sqlParam, Control ctrlPage, string widgetAfRowId)
    {
        Hashtable hsParamNew = new Hashtable();
        try
        {
            Hashtable hsParam = new Hashtable();
            ArrayList alOrder = new ArrayList();  // Added by 'Imran Gani' on 05-Jan-2014, to order parameters
            ArrayList alOrderIndex = new ArrayList();
            
            if (sqlParam.Trim() == "")
            {
                getParameters("$", ref hsParam, sqlQry);
                getParameters("#", ref hsParam, sqlQry);
            }
            else
            {
                if (sqlParam.Trim().ToLower().StartsWith("parameters"))
                    sqlParam = sqlParam.Substring("parameters".Length + 1).Trim();
                int PCount = 0; // Added by 'Imran Gani' on 05-Jan-2014, to get count of parameters
                foreach (string str1 in sqlParam.Split(','))
                {
                    if (str1.Trim() == "") continue;
                    PCount++;
                    string sParameter = getParameterKey(str1.Trim(), ref alOrderIndex); // Added by 'Imran Gani' on 28-Oct-2013, to get parameter key for hashtable.
                    if (sParameter.IndexOf("=") == -1)
                    {
                        hsParam.Add(sParameter.Trim(), getDefaultValue(sParameter.Substring(sParameter.IndexOf("]") + 1).Trim()));
                        alOrder.Add(str1.Trim());
                    }
                    else
                    {
                        hsParam.Add(sParameter.Substring(0, sParameter.IndexOf("=")).Trim(), sParameter.Substring(sParameter.IndexOf("=") + 1).Trim());
                        alOrder.Add(str1.Substring(0, str1.IndexOf("=")).Trim());
                    }
                    if (sParameter.IndexOf("=") == -1) // Added by 'Imran Gani' on 01-Aug-2013, to handle parameter values from URL
                        getParameterValueFromURL(ref hsParam, sParameter.Substring(0, sParameter.LastIndexOf("]") + 1).Trim(), widgetAfRowId, PCount);
                    else
                        getParameterValueFromURL(ref hsParam, sParameter.Substring(0, sParameter.IndexOf("=")).Trim(), widgetAfRowId, PCount);
                }
            }
            ReplaceSysVariables(ref hsParam); // Added by 'Imran Gani' on 09-Jul-2012, to replace the system variables with their values.
            alOrder = getParameterOrder(alOrder, alOrderIndex); // Added by 'Imran Gani' on 28-Oct-2013, to arrange parameter order by sequence.
            int iCnt = 1, ParamCount = 0;
            DynamicDisplayI ddI = new DynamicDisplayI();
            #region Old parameter style
            if (alOrder.Count == 0)
            {
                foreach (string key in hsParam.Keys)
                {
                    //string ch = key.StartsWith("$") ? "$" : "#";
                    TextBox txt = (TextBox)ddI.FindControl(ctrlPage.Controls, "txt_param_" + iCnt.ToString() + widgetAfRowId);
                    if (txt != null)
                    {
                        if (txt.Text.Trim() == "")
                        {
                            if (key.StartsWith("$"))
                                hsParamNew.Add(key, "0");
                            else if (key.StartsWith("#"))
                                hsParamNew.Add(key, DateTime.Now.ToShortDateString());
                        }
                        else
                            hsParamNew.Add(key, "" + txt.Text + "");
                        iCnt++;
                    }
                }
            }
            #endregion
            else
            {
                foreach (string key in alOrder)
                {
                    if (sqlParam.Trim() != "")
                    {
                        if (key.StartsWith("$") || key.StartsWith("#"))
                        {
                            //string ch = key.StartsWith("$") ? "$" : "#";
                            TextBox txt = (TextBox)ddI.FindControl(ctrlPage.Controls, "txt_param_" + iCnt.ToString() + widgetAfRowId);
                            if (txt != null)
                            {
                                if (txt.Text.Trim() == "")
                                    hsParamNew.Add(key, "'0'");
                                else
                                    hsParamNew.Add(key, txt.Text);
                            }
                        }
                        else
                        {
                            string parName = key.Substring(0, key.LastIndexOf("["));
                            string par = key.Replace(parName, "").Replace("[", "").Replace("]", "");
                            string[] pars = par.Split('.');
                            HDDatabase hdd = new HDDatabase();
                            DataTable dt = hdd.GetDataTable("select * from sy_pg_ctr where page_id=(select af_row_id from sy_pg where [page_table_name]='" + pars[0] + "') and control_attrib_id='" + pars[1] + "'");
                            if (dt.Rows.Count > 0)
                            {
                                //creating control based on control_html
                                //EM,DD,TA,AN,TB,CB,DDE,PW,ENC
                                ParamCount++;
                                switch (dt.Rows[0]["control_html"].ToString().ToUpper())
                                {
                                    case "AN":
                                    case "EM":
                                    case "PW":
                                    case "ENC":
                                    case "TA":
                                    case "TB":
                                        Localization loc = new Localization(); // Added by 'Imran Gani' on 06-Mar-2014, for MBLocale
                                        TextBox txt = (TextBox)ddI.FindControl(ctrlPage.Controls, "txt_" + dt.Rows[0]["af_row_id"].ToString() + "_" + widgetAfRowId + "_" + ParamCount.ToString()); // Modified by 'Imran Gani' on 05-Jan-2014, replace 'key' => 'Control_row_id'
                                        if (txt != null)
                                        {
                                            HiddenField hf = (HiddenField)ddI.FindControl(ctrlPage.Controls, "hf_" + dt.Rows[0]["af_row_id"].ToString() + "_" + widgetAfRowId + "_" + ParamCount.ToString()); // Modified by 'Imran Gani' on 05-Jan-2014, replace 'key' => 'Control_row_id'
                                            if (hf != null)
                                            {
                                                if (hf.Value.Trim() == "")
                                                {
                                                    if (txt.Text.Trim() == "")
                                                        hsParamNew.Add(key, "'" + checkForEmptyValue(txt.Text.Trim(), dt.Rows[0]["control_attrib_type"].ToString()) + "'");
                                                    else
                                                    {
                                                        if (txt.Text.Trim().StartsWith("'") && txt.Text.Trim().EndsWith("'"))
                                                            hsParamNew.Add(key, loc.getDBValueFromLocale(txt.Text.Trim(), dt.Rows[0]["control_attrib_type"].ToString()));
                                                        else
                                                            hsParamNew.Add(key, "'" + loc.getDBValueFromLocale(txt.Text.Trim(), dt.Rows[0]["control_attrib_type"].ToString()) + "'");
                                                    }
                                                }
                                                else
                                                    hsParamNew.Add(key, "'" + loc.getDBValueFromLocale(hf.Value.Trim(), dt.Rows[0]["control_attrib_type"].ToString()) + "'");
                                            }
                                            else
                                            {
                                                if (txt.Text.Trim().StartsWith("'") && txt.Text.Trim().EndsWith("'"))
                                                    hsParamNew.Add(key, loc.getDBValueFromLocale(txt.Text.Trim(), dt.Rows[0]["control_attrib_type"].ToString()));
                                                else
                                                {
                                                    if (txt.Text.Trim() != "")
                                                        hsParamNew.Add(key, "'" + loc.getDBValueFromLocale(txt.Text.Trim(), dt.Rows[0]["control_attrib_type"].ToString()) + "'");
                                                    else
                                                        hsParamNew.Add(key, "'" + checkForEmptyValue(txt.Text.Trim(), dt.Rows[0]["control_attrib_type"].ToString()) + "'");
                                                }
                                            }
                                        }
                                        break;
                                    case "CB":
                                        CheckBox cb = (CheckBox)ddI.FindControl(ctrlPage.Controls, "cb_" + dt.Rows[0]["af_row_id"].ToString() + "_" + widgetAfRowId + "_" + ParamCount.ToString()); // Modified by 'Imran Gani' on 05-Jan-2014, replace 'key' => 'Control_row_id'
                                        if (cb != null)
                                            hsParamNew.Add(key, "'" + cb.Checked.ToString() + "'");
                                        break;
                                    case "DD":
                                    case "DDE":
                                    case "DL":
                                        DropDownList dd = (DropDownList)ddI.FindControl(ctrlPage.Controls, "dd_" + dt.Rows[0]["af_row_id"].ToString() + "_" + widgetAfRowId + "_" + ParamCount.ToString()); // Modified by 'Imran Gani' on 05-Jan-2014, replace 'key' => 'Control_row_id'
                                        if (dd != null)
                                        {
                                            if (dd.Text.Trim() == "")
                                                hsParamNew.Add(key, "'0'");
                                            else
                                                hsParamNew.Add(key, "'" + dd.Text + "'");
                                        }
                                        break;
                                    case "DT":
                                    case "DA":
                                        TextBox dadt = (TextBox)ddI.FindControl(ctrlPage.Controls, "dt_" + dt.Rows[0]["af_row_id"].ToString() + "_" + widgetAfRowId + "_" + ParamCount.ToString()); // Modified by 'Imran Gani' on 05-Jan-2014, replace 'key' => 'Control_row_id'
                                        if (dadt != null)
                                        {
                                            if (dadt.Text.Trim() == "")
                                                hsParamNew.Add(key, "'" + DateTime.Now.ToShortDateString() + "'");
                                            else
                                                hsParamNew.Add(key, "'" + dadt.Text + "'");
                                        }
                                        break;
                                }
                            }
                        }
                    }
                    else
                    {
                        //string ch = key.StartsWith("$") ? "$" : "#";
                        TextBox txt = (TextBox)ddI.FindControl(ctrlPage.Controls, "txt_param_" + iCnt.ToString() + widgetAfRowId);
                        if (txt != null)
                        {
                            if (txt.Text.Trim() == "")
                            {
                                if (key.StartsWith("$"))
                                    hsParamNew.Add(key, "0");
                                else if (key.StartsWith("#"))
                                    hsParamNew.Add(key, DateTime.Now.ToShortDateString());
                            }
                            else
                                hsParamNew.Add(key, "" + txt.Text + "");
                            iCnt++;
                        }
                    }
                }
            }
            ddI = null;
            if (hsParamNew.Count == 0)
                hsParamNew = hsParam;
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("BuildParameters: updateParameters : Error : " + ex.Message);
        }
        return hsParamNew;
    }

    /// <summary>
    /// function to generate SQL query with replacing parameter values
    /// </summary>
    /// <param name="hsParam"></param>
    /// <returns></returns>
    public string makeSQL(Hashtable hsParam, string sqlQry)
    {
        string retQry = sqlQry;
        try
        {
            foreach (string sParam in hsParam.Keys)
            {
                if (sParam.Trim() == "") continue;
                if (sParam.IndexOf("[") == -1)
                {
                    string tmpP = sParam;
                    string outVal = checkSingleQuote(tmpP, hsParam[sParam].ToString(), retQry);
                    outVal = outVal.Replace("'", "''");
                    //outVal = HttpUtility.HtmlEncode(outVal); // Added by 'Imran Gani' on 29-Oct-12, to encode the input value.
                    retQry = retQry.Replace(tmpP, outVal);

                    tmpP = sParam;
                    outVal = checkSingleQuote(tmpP, hsParam[sParam].ToString(), retQry);
                    retQry = retQry.Replace(tmpP, outVal);
                }
                else
                {
                    string tmpP = "$" + sParam.Substring(0, sParam.LastIndexOf("[")).Replace("[", "").Replace("]", "") + "$";
                    string outVal = checkSingleQuote(tmpP, hsParam[sParam].ToString(), retQry);
                    outVal = outVal.Replace("'", "''");
                    //outVal = HttpUtility.HtmlEncode(outVal); // Added by 'Imran Gani' on 29-Oct-12, to encode the input value.
                    retQry = retQry.Replace(tmpP, outVal);

                    tmpP = "#" + sParam.Substring(0, sParam.LastIndexOf("[")).Replace("[", "").Replace("]", "") + "#";
                    outVal = checkSingleQuote(tmpP, hsParam[sParam].ToString(), retQry);
                    retQry = retQry.Replace(tmpP, outVal);
                }
            }

           retQry = ReplaceVariablesinQueries(retQry);
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("BuildParameters : makeSQL : Error : " + ex.Message);
        }
        return retQry;
    }

    private string checkSingleQuote(string parName, string inputVal, string qry)
    {
        string outVal = inputVal;
        try
        {
            if (qry.IndexOf(parName) != -1)
            {
                string tmp = qry.Substring(qry.IndexOf(parName) - 3, 3 + parName.Length);
                if (tmp.Contains("%") || tmp.Contains("'"))
                    outVal = inputVal.TrimStart('\'').TrimEnd('\'');
            }
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("BuildParameters: checkSingleQuote: Error:" + ex.Message);
        }
        return outVal;
    }

    /// <summary>
    /// This will get the Parameters in referenced Hashtable
    /// </summary>
    /// <param name="sqlQry"></param>
    /// <param name="sqlParam"></param>
    /// <param name="hsParam"></param>
    public void GetParametersInHashTable(string sqlQry, string sqlParam, ref Hashtable hsParam) //Added by Srivatsan
    {
        ArrayList alOrderIndex = new ArrayList();
        try
        {
            if (sqlParam.Trim() == "")
            {
                getParameters("$", ref hsParam, sqlQry);
                getParameters("#", ref hsParam, sqlQry);
            }
            else
            {
                if (sqlParam.Trim().ToLower().StartsWith("parameters"))
                    sqlParam = sqlParam.Substring("parameters".Length + 1).Trim();
                int PCount = 0; // Added by 'Imran Gani' on 05-Jan-2014, to get count of parameters
                foreach (string str1 in sqlParam.Split(','))
                {
                    if (str1.Trim() == "") continue;
                    PCount++;
                    string sParameter = getParameterKey(str1.Trim(), ref alOrderIndex); // Added by 'Imran Gani' on 28-Oct-2013, to get parameter key for hashtable.
                    if (sParameter.IndexOf("=") == -1)
                        hsParam.Add(sParameter.Trim(), getDefaultValue(sParameter.Substring(sParameter.IndexOf("]") + 1).Trim()));
                    else
                        hsParam.Add(sParameter.Substring(0, sParameter.IndexOf("=")).Trim(), sParameter.Substring(sParameter.IndexOf("=") + 1).Trim());
                    if (sParameter.IndexOf("=") == -1) // Added by 'Imran Gani' on 01-Aug-2013, to handle parameter values from URL
                        getParameterValueFromURL(ref hsParam, sParameter.Substring(0, sParameter.LastIndexOf("]") + 1).Trim(), "", PCount);
                    else
                        getParameterValueFromURL(ref hsParam, sParameter.Substring(0, sParameter.IndexOf("=")).Trim(), "", PCount);
                }
            }

        }
        catch(Exception ex)
        {
            LogWriter.WriteLog("BuildParameter : GetParametersInHashTable :"+ ex.Message);
        }
    }

    /// <summary>
    /// This will add Close button to the Parameter section
    /// </summary>
    /// <param name="af_row_id"></param>
    /// <param name="htcresult"></param>
    private void AddCloseButton(string af_row_id,ref HtmlTableCell htcresult)
    {
        try
        {
            HtmlTable ht = new HtmlTable();
            HtmlTableRow htr = new HtmlTableRow();
            HtmlTableCell htc = new HtmlTableCell();
            //LiteralControl litcon = new LiteralControl();
            //litcon.Text = "<img src=\"Images/close.gif\" alt=\"Close\" onclick=\"javascript:HideDivForActions('Param_"+af_row_id+ "');\"><img>";
            Image img = new Image();
            img.ImageUrl = "Images/close_15.GIF";
            img.Attributes.Add("onclick", "javascript:HideDivForActions('Param_" + af_row_id + "');");
            htc.Controls.Add(img);
            htr.Controls.Add(htc);
            ht.Controls.Add(htr);

            htcresult.Controls.Add(ht);
        }
        catch(Exception ex)
        {
            LogWriter.WriteLog("BuildParameters : AddCloseButton :"+ ex.Message);
        }
    }
    private void CreateParameterHeader(ref HtmlTableCell htcresult,string widgetAfRowId)
    {
        try
        {
            HtmlTable ht = new HtmlTable();
            HtmlTableRow htr = new HtmlTableRow();
            HtmlTableCell htc = new HtmlTableCell();
            ht.CellPadding = 3;
            ht.CellSpacing = 0;
            ht.Border = 0;
            ht.Width = "100%";
            
            Label lbl1 = new Label();
            lbl1.Text = "Parameters";
            lbl1.CssClass = "controlLabelFont";
            htc = new HtmlTableCell();
            htc.Attributes.Add("class", "displaygridbottom");
            htc.Controls.Add(lbl1);
            htr.Controls.Add(htc);

            htc = new HtmlTableCell();
            htc.Attributes.Add("class", "displaygridbottom");
            lbl1 = new Label();
            lbl1.Text = "";
            lbl1.CssClass = "controlLabelFont";
            htc.Controls.Add(lbl1);
            htr.Controls.Add(htc);


            htc = new HtmlTableCell();
            htc.Align = "right";
            htc.Attributes.Add("class", "displaygridbottom");
            AddCloseButton(widgetAfRowId, ref htc);
            htr.Controls.Add(htc);
            ht.Controls.Add(htr);
            
            htcresult.Controls.Add(ht);

        }
        catch(Exception ex)
        {
            LogWriter.WriteLog("BuildParameters : CreateParameterHeader :"+ ex.Message);
        }
    }

    // Convert private to public by 'Imran Gani' on 20-Nov-12.
    /// <summary>
    /// To replace system variables, if exist in query.
    /// </summary>
    /// <param name="sql">Current sql query</param>
    /// <returns>Modified sql query</returns>
    public string ReplaceVariablesinQueries(string sql)
    {
        try
        {
            if (HttpContext.Current.Session["sysvar"] != null)
            {
                Hashtable hshvar = (Hashtable)HttpContext.Current.Session["sysvar"];

                if (hshvar != null)
                {
                    if (sql.Trim().Contains("$") || sql.Trim().Contains("@") || sql.Trim().Contains("!"))
                    {
                        Hashtable hashtemp = new Hashtable();
                        string tempsql = sql;
                        foreach (string a in hshvar.Keys)
                        {
                            if (a.Contains("."))
                            {
                                string[] arr = a.Split('.');
                                if (arr.Length > 0)
                                {
                                    int len = arr.Length;
                                    hashtemp.Add("$" + arr[len - 1].Trim() + "$", hshvar[a].ToString());
                                }
                            }
                            else
                            {
                                hashtemp.Add(a, hshvar[a].ToString());
                            }
                        }

                        security hdds = new security();
                        hdds.loadAppVariables(ref hashtemp);


                        foreach (string s in hashtemp.Keys)
                        {
                            if (tempsql.Contains(s))
                            {
                                tempsql = tempsql.Replace(s, hashtemp[s].ToString());
                            }
                        }

                        if (tempsql.Trim() != "")
                        {
                            sql = tempsql;
                        }
                    }

                }
            }
            else
            {
                //security hdds = new security();
                Hashtable hsSysVar = new Hashtable();
                loadSystemVariables(ref hsSysVar);
                //hdds.loadAppVariables(ref hsSysVar);
                HttpContext.Current.Session["sysvar"] = hsSysVar;
                sql = ReplaceVariablesinQueries(sql);
            }
        }
        catch
        {

        }
         
        return sql;
    }

    private void loadSystemVariables(ref Hashtable sysVar)
    {
        Master objmaster = new Master();
        try
        {
            DataSet ds = new DataSet();
            string dsTemp = "";
            HDDatabase db = new HDDatabase();
            ds = db.GetDataset("select * from sy_var");
            if (ds.Tables.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    switch (dr["sy_var_type"].ToString())
                    {
                        case "DQ"://database query
                            dsTemp = db.GetColumnValue(objmaster.ReplaceVariables(dr["sy_var_value"].ToString(), ref sysVar));

                            if (sysVar.Contains("{!" + dr["sy_var_name"].ToString() + "}"))
                            {
                                if (dsTemp.ToLower() != sysVar["{!" + dr["sy_var_name"].ToString() + "}"].ToString().Trim())
                                {
                                    if (dsTemp.ToString().Trim() != "")
                                    //if (sysVar["{!" + dr["sy_var_name"].ToString() + "}"].ToString().Trim() == "" || sysVar["{!" + dr["sy_var_name"].ToString() + "}"].ToString().Trim() == "0" || sysVar["{!" + dr["sy_var_name"].ToString() + "}"].ToString().Trim() == "0.0000" || sysVar["{!" + dr["sy_var_name"].ToString() + "}"].ToString().Trim() == "0.00")
                                    {
                                        sysVar["{!" + dr["sy_var_name"].ToString() + "}"] = dsTemp;
                                    }
                                }
                            }
                            else
                            {
                                sysVar.Add("{!" + dr["sy_var_name"].ToString() + "}", dsTemp);
                            }
                            //if (dsTemp.Tables.Count > 0)
                            //    sysVar.Add("{!" + dr["sy_var_name"].ToString() + "}", dsTemp.Tables[0].Rows[0][0].ToString());
                            //else
                            //    sysVar.Add("{!" + dr["sy_var_name"].ToString() + "}", dr["sy_var_defaultvalue"]);
                            break;
                        case "CO"://constant
                            if (dr["sy_var_value"].ToString().Length > 0)
                                sysVar.Add("{!" + dr["sy_var_name"].ToString() + "}", dr["sy_var_value"]);
                            else
                                sysVar.Add("{!" + dr["sy_var_name"].ToString() + "}", dr["sy_var_defaultvalue"]);
                            break;
                    }
                }
            }
            ds = null;
            db = null;
        }
        catch (Exception e)
        {
            LogWriter.WriteLog("Error in loadSystemVariables function: " + e.Message);
        }
        finally
        {
            objmaster = null;
        }
    }

    /// <summary>
    /// To replace the system variables with their values
    /// </summary>
    /// <param name="hsParam">Parameter HashTable</param>
    private void ReplaceSysVariables(ref Hashtable hsParam) // Implemented by 'Imran Gani' on 09-Jul-2012
    {
        try
        {
            ArrayList keys = new ArrayList();
            foreach (string de in hsParam.Keys)
                keys.Add(de);

            foreach (string strParam in keys)
            {
                if (hsParam[strParam].ToString().StartsWith("{!") && hsParam[strParam].ToString().EndsWith("}"))
                {
                    Hashtable hsSysVar = new Hashtable();
                    if (HttpContext.Current.Session["sysvar"] == null)
                    {
                        loadSystemVariables(ref hsSysVar);
                        HttpContext.Current.Session["sysvar"] = hsSysVar;
                    }
                    else
                        hsSysVar = (Hashtable)HttpContext.Current.Session["SysVar"];
                    if (hsSysVar.Contains(hsParam[strParam].ToString()))
                        hsParam[strParam] = hsSysVar[hsParam[strParam].ToString()].ToString();
                }
                else if (hsParam[strParam].ToString().StartsWith("{@") && hsParam[strParam].ToString().EndsWith("}"))
                {
                    security hds = new security();
                    Hashtable hsAppVar = new Hashtable();
                    hds.loadAppVariables(ref hsAppVar);
                    hds = null;
                    if (hsAppVar.Contains(hsParam[strParam].ToString()))
                        hsParam[strParam] = hsAppVar[hsParam[strParam].ToString()].ToString();
                }
                else if (hsParam[strParam].ToString() == "CurrentDate()")
                {
                    hsParam[strParam] = DateTime.Now.ToShortDateString();
                }
            }
        }
        catch (Exception e)
        {
            LogWriter.WriteLog("Error in BuildParameters : ReplaceSysVariables: " + e.Message);
        }
        finally
        {
        }
    }
    
    /// <summary>
    /// To handle the parameter values from URL, which will override the HashTable.
    /// </summary>
    /// <param name="htParam">HashTable of a parameter.</param>
    /// <param name="sKey">Key of HashTable list.</param>
    /// <param name="widgetAfRowId">af_row_id of widget.</param>
    /// <param name="ParamCount">Count of paramter.</param>
    // Added by 'Imran Gani' on 01-Aug-2013, to handle parameter values from URL // Modified by 'Imran Gani' on 05-Jan-2014
    private void getParameterValueFromURL(ref Hashtable htParam, string sKey, string widgetAfRowId, int ParamCount)
    {
        try
        {
            string parName = sKey.Substring(0, sKey.LastIndexOf("["));
            string par = sKey.Replace(parName, "").Replace("[", "").Replace("]", "");
            string[] pars = par.Split('.');
            string sUrlKey = "";
            HDDatabase hdd = new HDDatabase();
            DataTable dt = hdd.GetDataTable("select * from sy_pg_ctr where page_id=(select af_row_id from sy_pg where [page_table_name]='" + pars[0] + "') and control_attrib_id='" + pars[1] + "'");
            if (dt.Rows.Count > 0)
            {
                switch (dt.Rows[0]["control_html"].ToString().ToUpper())
                {
                    case "AN":
                    case "EM":
                    case "PW":
                    case "ENC":
                    case "TA":
                    case "TB":
                        sUrlKey = "txt_" + dt.Rows[0]["af_row_id"].ToString() + "_" + widgetAfRowId + "_" + ParamCount.ToString();
                        break;
                    case "CB":
                        sUrlKey = "cb_" + dt.Rows[0]["af_row_id"].ToString() + "_" + widgetAfRowId + "_" + ParamCount.ToString();
                        break;
                    case "DD":
                    case "DDE":
                        sUrlKey = "dd_" + dt.Rows[0]["af_row_id"].ToString() + "_" + widgetAfRowId + "_" + ParamCount.ToString();
                        break;
                    case "DT":
                    case "DA":
                        sUrlKey = "dt_" + dt.Rows[0]["af_row_id"].ToString() + "_" + widgetAfRowId + "_" + ParamCount.ToString();
                        break;
                }
            }
            if (HttpContext.Current.Request.QueryString[sUrlKey] != null && HttpContext.Current.Request.QueryString[sUrlKey].ToString() != "")
                htParam[sKey] = HttpContext.Current.Request.QueryString[sUrlKey].ToString();
            //if (HttpContext.Current.Request.QueryString[sKey.Replace(" ", "_")] != null && HttpContext.Current.Request.QueryString[sKey.Replace(" ", "_")].ToString() != "")
            //    htParam[sKey] = HttpContext.Current.Request.QueryString[sKey.Replace(" ", "_")].ToString();
        }
        catch (Exception e)
        {
            LogWriter.WriteLog("Error in BuildParameters : getParameterValueFromURL: " + e.Message);
        }
    }

    /// <summary>
    /// To get parameter from new syntax with sequence.
    /// </summary>
    /// <param name="sParam">String parameter</param>
    /// <param name="alOrderIndex">arraylist of sequence</param>
    /// <returns>String parameter without sequence</returns>
    // Added by 'Imran Gani' on 28-Oct-2013, to get parameter.
    private string getParameterKey(string sParam, ref ArrayList alOrderIndex)
    {
        string sReturn = sParam;
        try
        {
            string[] sChk = { "][" };
            if (sParam.Split(sChk, StringSplitOptions.None).Length > 2)
            {
                alOrderIndex.Add(sParam.Substring(sParam.IndexOf("[") + 1, sParam.IndexOf("]") - 1));
                sReturn = sParam.Substring(sParam.IndexOf("][") + 1);
            }
        }
        catch (Exception e)
        {
            LogWriter.WriteLog("Error in BuildParameters : getParameterKey: " + e.Message);
        }
        return sReturn;
    }

    /// <summary>
    /// To arrange parameter ordering, which display at parameter popup window.
    /// </summary>
    /// <param name="alOrder">List of parameter</param>
    /// <param name="alOrderIndex">list of index (sequence)</param>
    /// <returns>ArrayList of sequenced parameter.</returns>
    // Added by 'Imran Gani' on 28-Oct-2013, to get parameter.
    private ArrayList getParameterOrder(ArrayList alOrder, ArrayList alOrderIndex)
    {
        ArrayList alReturn = new ArrayList();
        try
        {
            if (alOrderIndex.Count == 0) return alOrder;
            getBubbleSortArrayList(ref alOrderIndex);
            string[] sChk = { "][" };
            foreach (string index in alOrderIndex)
            {
                foreach (string slist in alOrder)
                {
                    if (slist.StartsWith("[" + index + "]"))
                    {
                        alReturn.Add(slist.Substring(slist.IndexOf("]") + 1));
                        alOrder.Remove(slist);
                        break;
                    }
                }
            }
        }
        catch (Exception e)
        {
            LogWriter.WriteLog("Error in BuildParameters : getParameterOrder: " + e.Message);
        }
        return alReturn;
    }

    /// <summary>
    /// To re-arraange the order based on decimal values.
    /// </summary>
    /// <param name="alOrderIndex">ArrayList with decimal values.</param>
    // Added by 'Imran Gani' on 28-Oct-2013, to get parameter.
    private void getBubbleSortArrayList(ref ArrayList alOrderIndex)
    {
        try
        {
            string temp;
            for (int write = 0; write < alOrderIndex.Count; write++)
            {
                for (int sort = 0; sort < alOrderIndex.Count - 1; sort++)
                {
                    if (Convert.ToDouble(alOrderIndex[sort]) > Convert.ToDouble(alOrderIndex[sort + 1]))
                    {
                        temp = alOrderIndex[sort + 1].ToString();
                        alOrderIndex[sort + 1] = alOrderIndex[sort];
                        alOrderIndex[sort] = temp;
                    }
                }
            }
        }
        catch (Exception e)
        {
            LogWriter.WriteLog("Error in BuildParameters : getBubbleSortArrayList: " + e.Message);
        }
    }
    /// <summary>
    /// function to create horizontal parameters
    /// </summary>
    /// <param name="bClose">True for Dashboards</param>
    // Implemented by 'Imran Gani' on 22-Jan-2014.
    public HtmlTable createHorizontalParameters(string sqlQry, string sqlParam, Control ctrlPage, string widgetAfRowId, bool bClose)
    {
        HDDatabase hdd = new HDDatabase();

        HtmlTable tblParam = new HtmlTable();
        tblParam.Border = 0;
        tblParam.CellPadding = 0;
        tblParam.CellSpacing = 0;
        tblParam.Width = "100%";

        try
        {
            DynamicDisplayI ddI = new DynamicDisplayI();
            Hashtable hsParam = new Hashtable();
            ArrayList alOrder = new ArrayList();
            ArrayList alOrderIndex = new ArrayList();

            if (sqlParam.Trim() == "")
            {
                if (sqlQry.Contains("$") || sqlQry.Contains("#"))
                {
                    getParameters("$", ref hsParam, sqlQry);
                    getParameters("#", ref hsParam, sqlQry);
                }
            }
            else
            {
                if (sqlParam.Trim().ToLower().StartsWith("parameters"))
                    sqlParam = sqlParam.Substring("parameters".Length + 1).Trim();
                int PCount = 0;
                foreach (string str1 in sqlParam.Split(','))
                {
                    if (str1.Trim() == "") continue;
                    PCount++;
                    string sParameter = getParameterKey(str1.Trim(), ref alOrderIndex);
                    if (sParameter.IndexOf("=") == -1)
                    {
                        hsParam.Add(sParameter.Trim(), getDefaultValue(sParameter.Substring(sParameter.IndexOf("]") + 1).Trim()));
                        alOrder.Add(str1.Trim());
                    }
                    else
                    {
                        hsParam.Add(sParameter.Substring(0, sParameter.IndexOf("=")).Trim(), sParameter.Substring(sParameter.IndexOf("=") + 1).Trim());
                        alOrder.Add(str1.Substring(0, str1.IndexOf("=")).Trim());
                    }
                    //if (sParameter.IndexOf("=") == -1) // Added by 'Imran Gani' on 01-Aug-2013, to handle parameter values from URL
                    //    getParameterValueFromURL(ref hsParam, sParameter.Substring(0, sParameter.LastIndexOf("]") + 1).Trim(), widgetAfRowId, PCount);
                    //else
                    //    getParameterValueFromURL(ref hsParam, sParameter.Substring(0, sParameter.IndexOf("=")).Trim(), widgetAfRowId, PCount);
                }
            }
            if (hsParam.Count > 0)
            {
                ReplaceSysVariables(ref hsParam); // Added by 'Imran Gani' on 09-Jul-2012, to replace the system variables with their values.
                alOrder = getParameterOrder(alOrder, alOrderIndex); // Added by 'Imran Gani' on 28-Oct-2013, to arrange parameter order by sequence.

                string strPageView = hdd.GetColumnValue("select mem_id from sy_acr_mem where acr_id = 'bfbd5b18ed9b6cc1' and af_row_id in ('479e02854beb4e4c','7d46e1fe28c38f6a') and mem_value1 = 'enabled';");
                string strRowCtrlCnt = hdd.GetColumnValue("select mem_value1 from sy_acr_mem where acr_id = 'bfbd5b18ed9b6cc1' and af_row_id = 'b5e661ac539a0012';");
                string strBr = ""; 
                if (strPageView == "")
                    strPageView = "caption_on_left";

                int intRowCtrlCnt = 0;
                if (strRowCtrlCnt == "")
                    intRowCtrlCnt = GlobalValues.DefaultControlPerRow;
                else
                    intRowCtrlCnt = Convert.ToInt32(strRowCtrlCnt);

                if (strPageView != "caption_on_left")
                    strBr = "<br>";

                HtmlTableRow htRow = new HtmlTableRow();
                HtmlTableCell htCell = new HtmlTableCell();

                HtmlTable httTable = new HtmlTable();
                httTable.Border = 0;
                httTable.CellPadding = 3;
                httTable.CellSpacing = 0;
                httTable.Width = "100%";

                HtmlTableRow httRow = new HtmlTableRow();
                HtmlTableCell httCell1 = new HtmlTableCell();
                HtmlTableCell httCell2 = new HtmlTableCell();
                Label lbl1 = new Label();
                
                if (alOrder.Count == 0)
                {
                    #region Old parameter style
                    int iCnt = 1;
                    foreach (string key in hsParam.Keys)
                    {
                        if (key.Trim().StartsWith("$") || key.Trim().StartsWith("#"))
                        {
                            string ch = key.StartsWith("$") ? "$" : "#";
                            lbl1 = new Label();
                            lbl1.Text = key.Replace(ch, "");
                            lbl1.CssClass = "controlLabelFont";
                            lbl1.ID = "lbl_" + key + widgetAfRowId;
                            httRow = new HtmlTableRow();
                            httCell1 = new HtmlTableCell();
                            httCell1.Align = "left";
                            httCell1.NoWrap = true;
                            httCell1.Controls.Add(lbl1);
                            if (bClose)
                                httCell1.Attributes.Add("class", "Grid_Row_actions");
                            httRow.Cells.Add(httCell1);
                            TextBox txt = new TextBox();
                            txt.ID = "txt_param_" + iCnt.ToString() + widgetAfRowId;
                            txt.CssClass = "controlFont controlWidth";
                            httCell2 = new HtmlTableCell();
                            if (bClose)
                                httCell2.Attributes.Add("class", "Grid_Row_actions");
                            httCell2.Controls.Add(txt);
                            httRow.Cells.Add(httCell2);
                            httTable.Rows.Add(httRow);
                            if (key.StartsWith("#"))
                            {
                                if (!bClose)
                                    addAjaxCalendarDate(txt.ID, ref httCell2);
                                else
                                    addAjaxCalendarDate(txt.ID, ref httCell2, true);
                            }
                            iCnt++;
                        }
                    }
                    #endregion
                }
                else
                {
                    #region new parameter style
                    int ParamCount = 0;
                    for (int i = 0; i < alOrder.Count; i = i + intRowCtrlCnt)
                    {
                        httRow = new HtmlTableRow();
                        for (int j = i; j < i + intRowCtrlCnt; j++)
                        {
                            if (j > alOrder.Count - 1) break;
                            string key = alOrder[j].ToString();
                            string parName = key.Substring(0, key.LastIndexOf("["));
                            string par = key.Replace(parName, "").Replace("[", "").Replace("]", "");
                            string[] pars = par.Split('.');
                            DataTable dt = hdd.GetDataTable("select * from sy_pg_ctr where page_id=(select af_row_id from sy_pg where [page_table_name]='" + pars[0] + "') and control_attrib_id='" + pars[1] + "'");
                            if (dt.Rows.Count > 0)
                            {
                                ParamCount++;
                                lbl1 = new Label();
                                lbl1.Text = parName.Replace("[", "").Replace("]", "") + strBr;// dt.Rows[0]["control_name"].ToString();
                                lbl1.CssClass = "controlLabelFont";
                                lbl1.ID = "lbl_" + dt.Rows[0]["af_row_id"].ToString() + "_" + widgetAfRowId + "_" + ParamCount.ToString(); // Modified by 'Imran Gani' on 05-Jan-2014, replace 'key' => 'Control_row_id'
                                httCell2 = new HtmlTableCell();
                                httCell2.Align = "left";
                                httCell2.Controls.Add(lbl1);
                                if (bClose)
                                    httCell2.Attributes.Add("class", "Grid_Row_actions");
                                
                                if (strPageView == "caption_on_left")
                                {
                                    httRow.Cells.Add(httCell2);
                                    httCell2 = new HtmlTableCell();
                                }
                                
                                //creating control based on control_html
                                //EM,DD,TA,AN,TB,CB,DDE,PW,ENC

                                switch (dt.Rows[0]["control_html"].ToString().ToUpper())
                                {
                                    case "AN":
                                    case "EM":
                                    case "PW":
                                    case "ENC":
                                    case "TA":
                                    case "TB":
                                        TextBox txt = new TextBox();
                                        txt.ID = "txt_" + dt.Rows[0]["af_row_id"].ToString() + "_" + widgetAfRowId + "_" + ParamCount.ToString(); // Modified by 'Imran Gani' on 05-Jan-2014, replace 'key' => 'Control_row_id'
                                        txt.CssClass = "controlFont controlWidth";

                                        //if (dt.Rows[0]["control_bind_id"].ToString().Trim() != "") //Added by Srivatsan for Getting bind value
                                        //{}

                                        if (dt.Rows[0]["control_html"].ToString().ToUpper() == "TA")
                                            txt.TextMode = TextBoxMode.MultiLine;
                                        if (hsParam[key].ToString().Trim() == "''" || (hsParam[key].ToString().Trim() == "0"))
                                            txt.Text = "";
                                        else
                                            txt.Text = hsParam[key].ToString().Replace("''", "'"); // string replaced by 'Imran Gani' on 29-Oct-12, to double codes.

                                        httCell2.Controls.Add(txt);
                                        if (dt.Rows[0]["control_html"].ToString().ToUpper() == "PW")
                                        {
                                            txt.TextMode = TextBoxMode.Password;
                                            Literal l = new Literal();
                                            l.Text = "&nbsp;<img src=\"Images/key.gif\" alt=\"Password Field\" border=\"0\" />";
                                            httCell2.Controls.Add(l);
                                        }
                                        if (dt.Rows[0]["control_bind_id"].ToString() != "")
                                        {
                                            DataTable dtTmp = hdd.GetDataTable("select * from sy_bnd where af_row_id = '" + dt.Rows[0]["control_bind_id"].ToString() + "';");
                                            if (dtTmp.Rows.Count > 0)
                                            {
                                                Image imgParam = new Image();
                                                imgParam.ID = "imglookup_" + dt.Rows[0]["af_row_id"].ToString() + "_" + widgetAfRowId + "_" + ParamCount.ToString(); // Modified by 'Imran Gani' on 28-Jan-2014,
                                                imgParam.ImageUrl = "~/Images/search.gif";
                                                imgParam.ToolTip = "Click here for Lookup Window";
                                                imgParam.CssClass = "SearchPopup";
                                                httCell2.Controls.Add(imgParam);

                                                HiddenField hfTxt = new HiddenField();
                                                httCell2.Controls.Add(hfTxt);
                                                hfTxt.ID = "hf_" + dt.Rows[0]["af_row_id"].ToString() + "_" + widgetAfRowId + "_" + ParamCount.ToString(); // Modified by 'Imran Gani' on 05-Jan-2014, replace 'key' => 'Control_row_id'
                                                if (bClose)
                                                {
                                                    imgParam.Attributes.Add("onclick", "javascript:openlookupwindow('" + hfTxt.ID + "','" + txt.ID + "','" + dt.Rows[0]["control_bind_id"].ToString() + "','','');");
                                                    txt.Attributes.Add("onchange", "javascript:GetBindValue('" + dt.Rows[0]["control_bind_id"].ToString() + "','ctl00_ContentPlaceHolder3_" + hfTxt.ID + "','ctl00_ContentPlaceHolder3_" + txt.ID + "')");
                                                }
                                                else
                                                    imgParam.Attributes.Add("onclick", "javascript:openlookupwindow('wz_Report_" + hfTxt.ID + "','wz_Report_" + txt.ID + "','" + dt.Rows[0]["control_bind_id"].ToString() + "','','');");//wz_Report_

                                                DataTable dt_bind = new DataTable();
                                                BindControl bc = new BindControl();
                                                dt_bind = hdd.GetDataTable(bc.GetBindSql(dt.Rows[0]["control_bind_id"].ToString()));
                                                if (dt_bind.Rows.Count > 0)
                                                {
                                                    DataRow[] drBind = dt_bind.Select("Val = '" + txt.Text + "'");
                                                    if (drBind.Length > 0)
                                                    {
                                                        hfTxt.Value = txt.Text;
                                                        txt.Text = drBind[0]["Fld"].ToString();
                                                    }
                                                }
                                            }
                                        }
                                        if (dt.Rows[0]["control_attrib_type"].ToString().ToUpper() == "DA")
                                        {
                                            char[] trimCh = { '\'' };
                                            if (txt.Text.StartsWith("'") && txt.Text.EndsWith("'"))
                                                txt.Text = txt.Text.TrimStart(trimCh).TrimEnd(trimCh);
                                            //ce.OnClientShowing = "hideC1Report";
                                            //ce.OnClientHidden = "showC1Report";
                                            //txt.Attributes.Add("onfocus", "javascript:hideC1Report()");
                                            //txt.Attributes.Add("onblur", "javascript:showC1Report()");
                                            if (!bClose)
                                                addAjaxCalendarDate(txt.ID, ref httCell2);
                                            else
                                                addAjaxCalendarDate(txt.ID, ref httCell2, true);
                                        }
                                        else if (dt.Rows[0]["control_attrib_type"].ToString().ToUpper() == "DT")
                                        {
                                            char[] trimCh = { '\'' };
                                            if (txt.Text.StartsWith("'") && txt.Text.EndsWith("'"))
                                                txt.Text = txt.Text.TrimStart(trimCh).TrimEnd(trimCh);
                                            //txt.Attributes.Add("onfocus", "javascript:hideC1Report()");
                                            //txt.Attributes.Add("onblur", "javascript:showC1Report()");
                                            if (!bClose)
                                                addAjaxCalendarDateTime(txt.ID, ref httCell2);
                                            else
                                                addAjaxCalendarDateTime(txt.ID, ref httCell2, true);
                                        }
                                        break;
                                    case "CB":
                                        CheckBox cb = new CheckBox();
                                        cb.ID = "cb_" + dt.Rows[0]["af_row_id"].ToString() + "_" + widgetAfRowId + "_" + ParamCount.ToString(); // Modified by 'Imran Gani' on 05-Jan-2014, replace 'key' => 'Control_row_id'
                                        cb.CssClass = "controlFont controlWidth";
                                        cb.Checked = Convert.ToBoolean(hsParam[key].ToString().Replace("'", ""));
                                        httCell2.Controls.Add(cb);
                                        break;
                                    case "DD":
                                    case "DDE":
                                        DropDownList dd = new DropDownList();
                                        dd.CssClass = "controlFont controlWidth";
                                        dd.ID = "dd_" + dt.Rows[0]["af_row_id"].ToString() + "_" + widgetAfRowId + "_" + ParamCount.ToString(); // Modified by 'Imran Gani' on 05-Jan-2014, replace 'key' => 'Control_row_id'
                                        //dd.Text = hsParam[key].ToString(); // Commented by 'Imran Gani' on 01-Aug-2013
                                        httCell2.Controls.Add(dd);
                                        if (dt.Rows[0]["control_bind_id"].ToString() != "")
                                        {
                                            DataTable dtTmp = hdd.GetDataTable("select * from sy_bnd where af_row_id = '" + dt.Rows[0]["control_bind_id"].ToString() + "';");
                                            if (dtTmp.Rows.Count > 0)
                                            {
                                                BindControl bc = new BindControl();
                                                bc.Bind(dt.Rows[0]["control_bind_id"].ToString(), dd, ref dtTmp, dt.Rows[0]);
                                            }
                                        }
                                        dd.SelectedValue = hsParam[key].ToString().Replace("'", ""); // Added by 'Imran Gani' on 01-Aug-2013
                                        break;
                                    case "DT":
                                    case "DA":
                                        TextBox dadt = new TextBox();
                                        dadt.CssClass = "controlFont controlWidth";
                                        dadt.Text = hsParam[key].ToString();
                                        dadt.ID = "dt_" + dt.Rows[0]["af_row_id"].ToString() + "_" + widgetAfRowId + "_" + ParamCount.ToString(); // Modified by 'Imran Gani' on 05-Jan-2014, replace 'key' => 'Control_row_id'
                                        if (dt.Rows[0]["control_html"].ToString().ToUpper() == "DA")
                                        {
                                            if (!bClose)
                                                addAjaxCalendarDate(dadt.ID, ref httCell2);
                                            else
                                                addAjaxCalendarDate(dadt.ID, ref httCell2, true);
                                        }
                                        else
                                        {
                                            if (!bClose)
                                                addAjaxCalendarDateTime(dadt.ID, ref httCell2);
                                            else
                                                addAjaxCalendarDateTime(dadt.ID, ref httCell2);
                                        }
                                        break;
                                    case "DL": // Added by 'Imran Gani' on 28-Jan-2014, for droplist support.
                                        DropDownList DDL_Droplist = new DropDownList();
                                        DDL_Droplist.CssClass = "controlFont controlWidth";
                                        DDL_Droplist.ID = "dd_" + dt.Rows[0]["af_row_id"].ToString() + "_" + widgetAfRowId + "_" + ParamCount.ToString();

                                        DataTable dt_Droplist = hdd.GetDataTable("select * from [sy_pg_ctr_droplist] where [page_id] = '" + dt.Rows[0]["page_id"].ToString() + "' and [control_id] = '" + dt.Rows[0]["af_row_id"].ToString() + "';");
                                        foreach (DataRow dr in dt_Droplist.Select("droplist_value in ('none','empty')"))
                                            dr["droplist_value"] = "";
                                        DDL_Droplist.DataSource = dt_Droplist;
                                        DDL_Droplist.DataTextField = "droplist_value";
                                        DDL_Droplist.DataValueField = "droplist_value";
                                        DDL_Droplist.DataBind();
                                        #region New commented code Added by 'Imran Gani' on 19-Mar-2014, to use switch case with MBLocale
                                        //foreach (DataRow dr in dt_Droplist.Rows) // Added by 'Imran Gani' on 19-Mar-2014, for MBLocale
                                        //{
                                        //    string sDLText = dr["droplist_value"].ToString();
                                        //    try
                                        //    {
                                        //        switch (dt.Rows[0]["control_attrib_type"].ToString().ToLower())
                                        //        {
                                        //            case "dt":
                                        //            case "da":
                                        //                sDLText = MBLocalization.getLocalizedDate(HttpContext.Current.Session["userlocale"].ToString(), DateTime.Parse(dr["droplist_value"].ToString()));
                                        //                break;
                                        //            case "nu":
                                        //            case "an":
                                        //                sDLText = MBLocalization.getLocalizedInteger(HttpContext.Current.Session["userlocale"].ToString(), int.Parse(dr["droplist_value"].ToString()));
                                        //                break;
                                        //            case "cu":
                                        //            case "dc":
                                        //            case "fl":
                                        //                sDLText = MBLocalization.getLocalizedDecimal(HttpContext.Current.Session["userlocale"].ToString(), double.Parse(dr["droplist_value"].ToString()));
                                        //                break;
                                        //        }
                                        //    }
                                        //    catch (Exception ex)
                                        //    {
                                        //        sDLText = dr["droplist_value"].ToString();
                                        //        dr["droplist_value"] = new Localization().getDBValueFromLocale(dr["droplist_value"].ToString(), dt.Rows[0]["control_attrib_type"].ToString());
                                        //    }
                                        //    DDL_Droplist.Items.Add(new ListItem(sDLText, dr["droplist_value"].ToString()));
                                        //}
                                        #endregion
                                        httCell2.Controls.Add(DDL_Droplist);
                                        if (hsParam[key].ToString().Replace("'", "") != "")
                                            DDL_Droplist.Items.FindByText(hsParam[key].ToString().Replace("'", "")).Selected = true;
                                        break;
                                }
                                if (bClose)
                                    httCell2.Attributes.Add("class", "Grid_Row_actions");
                                httRow.Cells.Add(httCell2);
                            }
                            dt = null;
                        }
                        httTable.Rows.Add(httRow);
                    }
                    #endregion
                }
                htCell.Controls.Add(httTable);
                htRow.Cells.Add(htCell);
                tblParam.Rows.Add(htRow);
            }
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("BuildParameters: createHorizontalParameters : Error : " + ex.Message);
        }
        return tblParam;
    }

    private string checkForEmptyValue(string sInput, string sDataType)
    {
        try
        {
            if (sInput == "")
            {
                switch (sDataType.ToLower())
                {
                    //case "dt":
                    //case "da":
                    //    sInput = DateTime.Now.ToShortDateString();
                    //    break;
                    case "cu":
                    case "dc":
                    case "nu":
                    case "an":
                        sInput = "0";
                        break;
                }
            }
        }
        catch (Exception ex)
        {
            LogWriter.WriteLog("BuildParameters: checkForEmptyValue : Error : " + ex.Message);
        }
        return sInput;
    }
}
